﻿using Anke.SHManage.BLL;
using Anke.SHManage.Model;
using Anke.SHManage.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Anke.SHManage.Web.Areas.CM.Controllers
{
    public class AccidentManageController : Controller
    {
        //
        // GET: /CM/AccidentManage/

        public ActionResult AccidentManageList(string PassCenterCode, string PassStationCode, string PassPlateNumber, string PassCarNumber)
        {
            this.ViewData["PassCenterCode"] = PassCenterCode;
            this.ViewData["PassStationCode"] = PassStationCode;
            this.ViewData["PassPlateNumber"] = PassPlateNumber;
            this.ViewData["PassCarNumber"] = PassCarNumber;

            E_StatisticsPermisson em = UserOperateContext.Current.getMaxPerForStatistics();
            this.ViewData["em"] = em;
            string selfWorkCode = UserOperateContext.Current.Session_UsrInfo.WorkCode;
            this.ViewData["selfWorkCode"] = selfWorkCode;
            string selfCenterID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSubCenterID;
            this.ViewData["selfCenterID"] = selfCenterID;
            string selfStationID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSationID;
            this.ViewData["selfStationID"] = selfStationID;
            UserOperateContext userOperateContext = new UserOperateContext();
            bool Insert = userOperateContext.getGongneng(1263);//判断登录人有无“新增”油卡加油权限
            if (Insert)
            {
                this.ViewData["RolePermissionInsert"] = "Insert";//登录人有新增油卡加油权限
            }

            return View();
        }

        #region 查询
        public ActionResult DataLoad() {
            string zcenterCode = Request.Form["zcenterCode"].ToString();
            string zstationCode = Request.Form["zstationCode"].ToString();
            string zAccidentLevel = Request.Form["zAccidentLevel"].ToString();
            string zResponsibility = Request.Form["zResponsibility"].ToString();
            string zambCode = Request.Form["zambCode"].ToString();
            string zambNumb = Request.Form["zambNumb"].ToString();

            DateTime zAccidentTimeStart = Convert.ToDateTime(Request.Form["zAccidentTimeStart"]);
            DateTime zAccidentTimeEnd = Convert.ToDateTime(Request.Form["zAccidentTimeEnd"]);

            E_StatisticsPermisson em = UserOperateContext.Current.getMaxPerForStatistics();
            string selfWorkCode = UserOperateContext.Current.Session_UsrInfo.WorkCode;
            string selfCenterID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSubCenterID;
            string selfStationID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSationID;



            //获取页容量
            int rows = int.Parse(Request.Form["rows"]);
            //获取请求的页码
            int page = int.Parse(Request.Form["page"]);
            //返回总行数
            int total = 0;
            var list = new C_AccidentBLL().getAccidentList(page, rows, ref total, zcenterCode, zstationCode, zAccidentLevel, zResponsibility, zambCode, zambNumb, zAccidentTimeStart, zAccidentTimeEnd , em,  selfWorkCode,  selfCenterID,  selfStationID);
            return Json(new { total = total, rows = list }, "appliction/json", JsonRequestBehavior.AllowGet);
        }

        #endregion

        #region 新增
        /// <summary>
        /// 新增方法
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SaveNewAccident(C_Accident info)
        {

            string mzaxID = new C_AccidentBLL().getMaxID(); //取最大ID，如果取到，就最大ID +1，如果没有就"00001"
            int maxID = Convert.ToInt32(mzaxID);
            if (maxID > 0)
            { info.ID = (maxID + 1).ToString("0000000000000"); }  //aa.ToString("000"); C#将int型数字转换成3位字符串，不足的时候，前面补0
            else { info.ID = "0000000000001"; }

            string ambID = Request.Form["ambid"].ToString();
            info.AmbulanceCode = ambID;

            if (new C_AccidentBLL().Add(info) > 0)
                return Json(new { Result = "OK", Message = "事故信息添加成功！" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);
            else
                return Json(new { Result = "Error", Message = "事故信息添加失败！" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 获取修改内容
     
        [HttpPost]
        public ActionResult getEditACD()
        {
            string ID = Request.Form["EditID"].ToString();

            C_Accident info = null;

            new C_AccidentBLL().getEditACD(ID, out info);
            return Json(new { Info = info }, "appliction/json", JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 修改
        [HttpPost]
        public ActionResult EditACD(C_Accident info)
        {
            string tempID = Request.Form["EditID"].ToString();
            info.ID = tempID;
            int aresult = new C_AccidentBLL().Modify(info, "CenterCode", "StationCode", "PlateNumber", "CarNumber", "AccidentTime", "AccidentAddress", "AccidentLevel", "Responsibility",
                "Description", "PersonName", "PersonWorkCode", "PersonCenterCode", "PersonStationCode", "DealExplain", "InsuranceExplain", "ReportNumber", "Remark", "EndReportPerson", "EndReportDate", "EndReportResult", "IFHurt");

            if (aresult > 0 )
            {
                return Json(new { Result = "OK", Message = "修改信息成功！" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);
               // return this.JsonResult(Utility.E_JsonResult.OK, "修改信息成功！", null, null);
            }
            else
            {
                return Json(new { Result = "Error", Message = "修改信息失败！" }, "text/plain;charset=UTF-8", JsonRequestBehavior.AllowGet);
              //  return this.JsonResult(Utility.E_JsonResult.Error, "修改信息失败！", null, null);
            }

        }

        #endregion

    }
}
